using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;

namespace MyNamespace
{
    public class User : Lookup
    {
        //Format: "1;#VPC1\\administrator"
        #region Constructors
        public User(ItemBase p_parentItem, string p_fieldName, string p_lookupField, int? p_valueId, bool p_emptyAllowed)
            : base(p_parentItem, p_fieldName, p_parentItem.item.Web.Site.RootWeb.ID, p_parentItem.item.Web.Site.RootWeb.SiteUserInfoList.ID, p_lookupField, p_valueId, p_emptyAllowed) { }

        public User(ItemBase p_parentItem, string p_fieldName, string p_lookupField, int? p_valueId, string p_valueString, bool p_emptyAllowed)
            : base(p_parentItem, p_fieldName, p_parentItem.item.Web.Site.RootWeb.ID, p_parentItem.item.Web.Site.RootWeb.SiteUserInfoList.ID, p_lookupField, p_valueId, p_valueString, p_emptyAllowed) { }

        public User(MultiLookup p_parentMultiSelect, int p_valueId)
            : base(p_parentMultiSelect, p_valueId) { }

        public User(MultiLookup p_parentMultiSelect, int p_valueId, string p_valueString)
            : base(p_parentMultiSelect, p_valueId, p_valueString) { }
        #endregion

        #region Methods
        public override void ChangeSelection(int newValueId)
        {
            SPFieldUser columnDef;
            if (partOfMultiSelect == true) columnDef = (SPFieldUser)parentMultiSelect.parentItem.item.ParentList.Fields.GetField(parentMultiSelect.fieldName);
            else columnDef = (SPFieldUser)parentItem.item.ParentList.Fields.GetField(fieldName);
            SPFieldUserSelectionMode selectionMode = columnDef.SelectionMode;
            if (selectionMode == SPFieldUserSelectionMode.PeopleOnly)
            {
                Type type = GetReferncedUserType(newValueId);
                if (type == Type.SharePointGroup || type == Type.DomainGroup) throw new PointBridgeEx("User.ChangeSelection(): '" + fieldName + "' user is 'People Only' while newValidId = " + newValueId + " points to a group.");
            }
            base.ChangeSelection(newValueId);
        }

        public enum Type { Person, DomainGroup, SharePointGroup };
        public Type GetReferncedUserType()
        {
            string ct = (string)GetReferencedItem()["ContentType"];
            try
            {
                return (Type)Enum.Parse(typeof(Type), ct);
            }
            catch (Exception ex)
            {
                throw new PointBridgeEx("User.GetReferncedUserType(): Unknown user type: '" + ct + "': " + ex.Message);
            }
        }
        public Type GetReferncedUserType(int valueId)
        {
            string ct = (string)GetReferencedItem(valueId)["ContentType"];
            try
            {
                return (Type)Enum.Parse(typeof(Type), ct);
            }
            catch (Exception ex)
            {
                throw new PointBridgeEx("User.GetReferncedUserType(): Unknown user type: '" + ct + "': " + ex.Message);
            }
        }
        public SPUser GetReferencedSPUser()
        {
            try
            {
                return web.SiteUsers.GetByID(valueId);
            }
            catch (Exception ex)
            {
                throw new PointBridgeEx("User.GetRegerncedSPUser(): Unable to find the Person. Most likely item is referncing a group. Error: " + ex.Message);
            }
        }
        public SPGroup GetReferncedSPGroup()
        {
            try
            {
                return web.SiteGroups.GetByID(valueId);
            }
            catch (Exception ex)
            {
                throw new PointBridgeEx("User.GetReferncedSPGroup(): Unable to find the SharePointGroup. Most likely item is referncing a Person or a DomainGroup. Error: " + ex.Message);
            }
        }
        #endregion
    }
}
